<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="depts" :props="defaultProps" default-expand-all>
        <!-- 使用具名函数自定义结构 -->
        <template #default="{ data }">
          <el-row type="flex" style="width: 100%; height: 40px" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span style="padding-right: 10px">{{ data.managerName }}</span>
              <el-dropdown @command="operatrDept($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <!-- 弹层位置 -->
      <add-dept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :parent-id="currentDeptId"
        @updateDept="getDepartmentList"
      />
    </div>
  </div>
</template>
<script>
import { deleteDepartmentAPI, getDepartmentsAPI } from '@/api/department'
import { transListToTreeData } from '@/utils'
import addDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: { addDept },
  data() {
    return {
      currentDeptId: null,
      // 控制弹层显示和隐藏
      showDialog: false,
      // 模拟的部门列表数据
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    operatrDept(type, currentDeptId) {
      this.currentDeptId = currentDeptId
      if (type === 'add') {
        this.showDialog = true
        this.currentDeptId = currentDeptId
      } else if (type === 'edit') {
        this.showDialog = true
        this.currentDeptId = currentDeptId
        // 父组件通知子组件使用部门id获取部门详情数据（同步）
        // 只有父组件才知道什么时候需要编辑部门
        // 需要等待监听的异步任务执行完才执行子组件的方法
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        // 弹出提示框
        this.$confirm('您确定要删除该部门吗', '', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(async() => {
          // 调用删除部门接口方法
          await deleteDepartmentAPI(currentDeptId)
          // 提示用户删除成功
          this.$message.success('删除部门成功')
          // 重新获取部门列表
          this.getDepartmentList()
        })
      }
    },
    // 调用获取部门列表方法
    async getDepartmentList() {
      const deptList = await getDepartmentsAPI()
      const treeList = transListToTreeData(deptList)
      this.depts = treeList
    }
  }
}
</script>
<style scoped>
  .app-container {
    padding: 30px 140px;
    font-size: 14px;
  }
</style>
